import java.io.File;
import java.util.Scanner;

import SFT.*;


public class Main {

	/**
	 * The main program.
	 * The user can either choose:
	 * "Hum" - to use the main algorithm, and trying to find a song from the Library by humming.
	 * "tuner" - in order to use a tuner.
	 * "add" - to add a song to the Library.
	 * "show" - to view information about the program's song Library.
	 * "quit" - in order to exit the program.
	 */
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String userInput = "";
		System.out.println("Welcome to Humming Bird, an SFT Algorithm implementation!");
		Library lib = new Library();
		do {
			System.out.println("What would you like to do?");
			System.out.println("'Hum' to use the Humming Bird function, 'Tuner' to use the tuner function, 'Add' to add another song to the Library, 'Show' to view details about the songs library, or 'Quit' to exit the program:");
			userInput = scan.next();
			
			if(userInput.toLowerCase().equals("hum")){//the user is using the "Hum" option
				System.out.println("Enter the file name and location for the HummingBird: ");
				userInput = scan.next();
				
				File input = new File(userInput);
				while(!input.exists()){
					System.out.println("File does not exsist, please try again");
					userInput = scan.next();
					input = new File(userInput);
				}
				WavFileInfo out = Integration.GetFrequencies(userInput, 0.1);
				MusicItem music = Integration.GetMusicItemFromFrequencies(out);
				MusicFile file = Integration.Algo(music, lib, 0);
				System.out.println("The suggested song the HummingBird found is: " + file.getName());
				
			}
			
			else if(userInput.toLowerCase().equals("tuner")){//the user is using the "Tuner" option
				System.out.println("Enter the file name and location for the Tuner: ");
				userInput = scan.next();
				
				File input = new File(userInput);
				while(!input.exists()){
					System.out.println("File does not exsist, please try again");
					userInput = scan.next();
					input = new File(userInput);
				}
				
				Tuner.tuneWAVFile(userInput);
			}
			
			else if(userInput.toLowerCase().equals("add")){//the user is using the "Add" option
				System.out.println("Enter the name of the song: ");
				userInput = scan.next();
				String name = userInput;
				System.out.println("Enter the file name and location for the new song: ");
				userInput = scan.next();
				
				File input = new File(userInput);
				while(!input.exists()){
					System.out.println("File does not exsist, please try again");
					userInput = scan.next();
					input = new File(userInput);
				}
				
				WavFileInfo out = Integration.GetFrequencies(userInput, 0.1);
				MusicItem music = Integration.GetMusicItemFromFrequencies(out);
				MusicFile file = new MusicFile(lib.getNum(), name, music);
				lib.SaveMusicFile(file);
			}
			
			else if(userInput.toLowerCase().equals("change")){//the user is using the "Add" option
				System.out.println("Enter the new num for the Library: ");
				userInput = scan.next();
				int num = Integer.parseInt(userInput);
				lib.SetLibreryNum(num);
			}
			
			else if(userInput.toLowerCase().equals("show")){//the user is using the "Add" option
				System.out.println("The number of songs in the Library is: " + lib.getNum());
				System.out.println("The songs in the Library are: ");
				for(int i = 0; i < lib.getNum(); i++){
					MusicFile mf = Library.LoadMusicFile(i);
					System.out.println(mf.getName());	
				}
			}
			
			else if(!userInput.toLowerCase().equals("quit")){//Bad Input
				System.out.println("Wrong Input, Please Try Again");
			}
			
		}
		while(!userInput.toLowerCase().equals("quit"));
		System.out.println("Thank You, Goodbye");
	}

}


